/*
 * 文件名： GoodsAdvertAdapter.java
 * 描    述：商品与广告
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.RelativeLayout;

import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.bean.AdvertBean;
import com.miguo.bean.GoodsAdvertBean;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.adapter.BaseAdapterHelper;
import com.miguo.framework.adapter.QuickAdapter;
import com.miguo.framework.utils.Log;
import com.miguo.framework.view.LazyImageView;
import com.miguo.ui.R;
import com.miguo.utils.Options;
import com.miguo.utils.ScreenUtils;
import com.miguo.utils.StringUtils;
import com.miguo.view.listener.SingleClickEvent;
import com.mrper.util.DeviceUtil;

public class GoodsAdvertAdapter extends QuickAdapter<GoodsAdvertBean> {

    private Activity mActivity;

    public GoodsAdvertAdapter(Activity activity) {
        super(activity, R.layout.list_goods);
        mActivity = activity;
    }

    @Override
    protected void convert(BaseAdapterHelper helper, GoodsAdvertBean item) {
        RelativeLayout imageRl = helper.getView(R.id.goods_image_rl);
        imageRl.getLayoutParams().height = mesureImage();

        if (item.getIsGoods()) {
            setGoods(helper, item.getGoods());
            setGoodsClickEvent(helper, item.getGoods());
        } else {
            setAdvert(helper, item.getAdvert());
            setAdvertClickEvent(helper, item.getAdvert());
        }
    }


    private void setGoods(BaseAdapterHelper helper, GoodsBean goods) {
        helper.setText(R.id.goods_title_tv, goods.getTitle());

        if (isFavor(goods)) {
            helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_sel);
        } else {
            helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_nor);
        }

        String CPrice;
        if (StringUtils.isNull(goods.getPhone_price())) {
            CPrice = goods.getCoupon_price();
        } else {
            CPrice = goods.getPhone_price();
        }

        helper.setText(R.id.goods_curprice_tv, CPrice);


        helper.setText(R.id.goods_volume_tv, Volume(goods.getVolume()));

        View.OnClickListener imgClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.performClick();
            }
        };
        LazyImageView img = helper.getView(R.id.goods_pic_iv);
        img.setNoImgMode(AppContext.isNoImg);
        img.autoDisplayImage(goods.getPic_url(),
                Options.getGoodsRoundedOptions(), imgClick);
    }

    private String Volume(Long volume) {
        String result = "0";
        if (volume < 1000) {
            result = volume + "";
        } else if (volume > 1000) {
            double volumeDouble = (double) volume / 1000;
            result = String.format("%.1f%s", volumeDouble, "K");
        }
        return result;
    }

    private void setGoodsClickEvent(final BaseAdapterHelper helper, final GoodsBean goods) {
        helper.setOnClickListener(R.id.goods_favor_rl, new SingleClickEvent() {
            @Override
            public void singleClick(View view) {
                if (isFavor(goods)) {
                    favorCancleAction(helper, goods);
                } else {
                    favorAction(helper, goods);
                }
            }
        });

        helper.getView().setOnClickListener(new SingleClickEvent() {

            @Override
            public void singleClick(View view) {
                AppHelper.startGoodsDetailPage((Activity)context,goods);//2016-03-14
//                if (AppContext.isAilError) {
//                    AppHelper.StartTBWebView(mActivity, goods, true);
//                } else {
//                    if (goods.getJump_type() == 2 || TextUtils.isEmpty(goods.getClick_url())) {
//                        //阿里百川
//                        BaiChuan.showTaokeItemDetailV2(mActivity, goods.getTitle(), goods.getItem_id(), goods.getItem_type());
//                    } else {
//                        if (goods.getJump_type() == 3) {
//                            BaiChuan.showPage(mActivity, goods.getTitle(), goods.getClick_url());
//                        } else {
//                            AppHelper.StartTBWebView(mActivity, goods, true);
//                        }
//                    }
//                }
            }

        });
    }

    private void setAdvert(BaseAdapterHelper helper, AdvertBean advert) {
        helper.setVisible(R.id.goods_favor_rl, View.GONE);

        helper.setText(R.id.goods_title_tv, advert.getTitle());
        helper.setText(R.id.goods_curprice_tv, advert.getSubject());
        View.OnClickListener imgClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.performClick();
            }
        };
        LazyImageView img = helper.getView(R.id.goods_pic_iv);
        img.setNoImgMode(AppContext.isNoImg);
        img.autoDisplayImage(advert.getItem_pic_url(),
                Options.getGoodsRoundedOptions(), imgClick);
    }

    private void setAdvertClickEvent(BaseAdapterHelper helper, final AdvertBean advert) {
        helper.getView().setOnClickListener(new SingleClickEvent() {

            @Override
            public void singleClick(View mView) {
                AppHelper.startActivityByType(mActivity, advert);
            }
        });
    }

    private void favorAction(BaseAdapterHelper helper, GoodsBean item) {
        GoodsDB.getInstance(context).save(item);
        helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_sel);
    }

    private void favorCancleAction(BaseAdapterHelper helper, GoodsBean item) {
        GoodsDB.getInstance(context).delete(item.getItem_id(), 1);
        helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_nor);
    }

    private boolean isFavor(GoodsBean item) {
        boolean isSetTip = false;
        try {
            isSetTip = GoodsDB.getInstance(context).isSave(item.getItem_id(),
                    1);
        } catch (Exception exception) {
            Log.d("" + exception.getMessage());
        }
        return isSetTip;
    }

    private int mesureImage() {
        int mPageWidth = DeviceUtil.getScreenWidth(context);
        return (mPageWidth - ScreenUtils.dip2px(mActivity, 21.0f)) / 2;//* 185 / 285;
    }
}
